(function() {
  // Technique from Juriy Zaytsev
  // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
  function isEventSupported(eventName) {
    var el = document.createElement('div');
    eventName = 'on' + eventName;
    var isSupported = (eventName in el);
    if (!isSupported) {
      el.setAttribute(eventName, 'return;');
      isSupported = typeof el[eventName] == 'function';
    }
    el = null;
    return isSupported;
  }

  function isForm(element) {
    return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
  }

  function isInput(element) {
    if (Object.isElement(element)) {
      var name = element.nodeName.toUpperCase()
      return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
    }
    else return false
  }

  var submitBubbles = isEventSupported('submit'),
      changeBubbles = isEventSupported('change')

  if (!submitBubbles || !changeBubbles) {
    // augment the Event.Handler class to observe custom events when needed
    Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
      function(init, element, eventName, selector, callback) {
        init(element, eventName, selector, callback)
        // is the handler being attached to an element that doesn't support this event?
        if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
             (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
          // "submit" => "emulated:submit"
          this.eventName = 'emulated:' + this.eventName
        }
      }
    )
  }

  if (!submitBubbles) {
    // discover forms on the page by observing focus events which always bubble
    document.on('focusin', 'form', function(focusEvent, form) {
      // special handler for the real "submit" event (one-time operation)
      if (!form.retrieve('emulated:submit')) {
        form.on('submit', function(submitEvent) {
          var emulated = form.fire('emulated:submit', submitEvent, true)
          // if custom event received preventDefault, cancel the real one too
          if (emulated.returnValue === false) submitEvent.preventDefault()
        })
        form.store('emulated:submit', true)
      }
    })
  }

  if (!changeBubbles) {
    // discover form inputs on the page
    document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
      // special handler for real "change" events
      if (!input.retrieve('emulated:change')) {
        input.on('change', function(changeEvent) {
          input.fire('emulated:change', changeEvent, true)
        })
        input.store('emulated:change', true)
      }
    })
  }

  function handleRemote(element) {
    var method, url, params;

    var event = element.fire("ajax:before");
    if (event.stopped) return false;

    if (element.tagName.toLowerCase() === 'form') {
      method = element.readAttribute('method') || 'post';
      url    = element.readAttribute('action');
      params = element.serialize();
    } else {
      method = element.readAttribute('data-method') || 'get';
      url    = element.readAttribute('href');
      params = {};
    }

    new Ajax.Request(url, {
      method: method,
      parameters: params,
      evalScripts: true,

      onComplete:    function(request) { element.fire("ajax:complete", request); },
      onSuccess:     function(request) { element.fire("ajax:success",  request); },
      onFailure:     function(request) { element.fire("ajax:failure",  request); }
    });

    element.fire("ajax:after");
  }

  function handleMethod(element) {
    var method = element.readAttribute('data-method'),
        url = element.readAttribute('href'),
        csrf_param = $$('meta[name=csrf-param]')[0],
        csrf_token = $$('meta[name=csrf-token]')[0];

    var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
    element.parentNode.insert(form);

    if (method !== 'post') {
      var field = new Element('input', { type: 'hidden', name: '_method', value: method });
      form.insert(field);
    }

    if (csrf_param) {
      var param = csrf_param.readAttribute('content'),
          token = csrf_token.readAttribute('content'),
          field = new Element('input', { type: 'hidden', name: param, value: token });
      form.insert(field);
    }

    form.submit();
  }


  document.on("click", "*[data-confirm]", function(event, element) {
    var message = element.readAttribute('data-confirm');
    if (!confirm(message)) event.stop();
  });

  document.on("click", "a[data-remote]", function(event, element) {
    if (event.stopped) return;
    handleRemote(element);
    event.stop();
  });

  document.on("click", "a[data-method]", function(event, element) {
    if (event.stopped) return;
    handleMethod(element);
    event.stop();
  });

  document.on("submit", function(event) {
    var element = event.findElement(),
        message = element.readAttribute('data-confirm');
    if (message && !confirm(message)) {
      event.stop();
      return false;
    }

    var inputs = element.select("input[type=submit][data-disable-with]");
    inputs.each(function(input) {
      input.disabled = true;
      input.writeAttribute('data-original-value', input.value);
      input.value = input.readAttribute('data-disable-with');
    });

    var element = event.findElement("form[data-remote]");
    if (element) {
      handleRemote(element);
      event.stop();
    }
  });

  document.on("ajax:after", "form", function(event, element) {
    var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
    inputs.each(function(input) {
      input.value = input.readAttribute('data-original-value');
      input.removeAttribute('data-original-value');
      input.disabled = false;
    });
  });

  Ajax.Responders.register({
    onCreate: function(request) {
      var csrf_meta_tag = $$('meta[name=csrf-token]')[0];

      if (csrf_meta_tag) {
        var header = 'X-CSRF-Token',
            token = csrf_meta_tag.readAttribute('content');

        if (!request.options.requestHeaders) {
          request.options.requestHeaders = {};
        }
        request.options.requestHeaders[header] = token;
      }
    }
  });
})();

(function(c) {
    function p(d, b, a) {
        var e = this,l = d.add(this),h = d.find(a.tabs),i = b.jquery ? b : d.children(b),j;
        h.length || (h = d.children());
        i.length || (i = d.parent().find(b));
        i.length || (i = c(b));
        c.extend(this, {click:function(f, g) {
            var k = h.eq(f);
            if (typeof f == "string" && f.replace("#", "")) {
                k = h.filter("[href*=" + f.replace("#", "") + "]");
                f = Math.max(h.index(k), 0)
            }
            if (a.rotate) {
                var n = h.length - 1;
                if (f < 0)return e.click(n, g);
                if (f > n)return e.click(0, g)
            }
            if (!k.length) {
                if (j >= 0)return e;
                f = a.initialIndex;
                k = h.eq(f)
            }
            if (f === j)return e;
            g = g || c.Event();
            g.type = "onBeforeClick";
            l.trigger(g, [f]);
            if (!g.isDefaultPrevented()) {
                o[a.effect].call(e, f, function() {
                    g.type = "onClick";
                    l.trigger(g, [f])
                });
                j = f;
                h.removeClass(a.current);
                k.addClass(a.current);
                return e
            }
        },getConf:function() {
            return a
        },getTabs:function() {
            return h
        },getPanes:function() {
            return i
        },getCurrentPane:function() {
            return i.eq(j)
        },getCurrentTab:function() {
            return h.eq(j)
        },getIndex:function() {
            return j
        },next:function() {
            return e.click(j + 1)
        },prev:function() {
            return e.click(j - 1)
        },destroy:function() {
            h.unbind(a.event).removeClass(a.current);
            i.find("a[href^=#]").unbind("click.T");
            return e
        }});
        c.each("onBeforeClick,onClick".split(","), function(f, g) {
            c.isFunction(a[g]) && c(e).bind(g, a[g]);
            e[g] = function(k) {
                k && c(e).bind(g, k);
                return e
            }
        });
        if (a.history && c.fn.history) {
            c.tools.history.init(h);
            a.event = "history"
        }
        h.each(function(f) {
            c(this).bind(a.event, function(g) {
                e.click(f, g);
                return g.preventDefault()
            })
        });
        i.find("a[href^=#]").bind("click.T", function(f) {
            e.click(c(this).attr("href"), f)
        });
        if (location.hash && a.tabs == "a" && d.find("[href=" + location.hash + "]").length)e.click(location.hash);
        else if (a.initialIndex === 0 || a.initialIndex > 0)e.click(a.initialIndex)
    }

    c.tools = c.tools || {version:"1.2.5"};
    c.tools.tabs = {conf:{tabs:"a",current:"current",onBeforeClick:null,onClick:null,effect:"default",initialIndex:0,event:"click",rotate:false,history:false},addEffect:function(d, b) {
        o[d] = b
    }};
    var o = {"default":function(d, b) {
        this.getPanes().hide().eq(d).show();
        b.call()
    },fade:function(d, b) {
        var a = this.getConf(),e = a.fadeOutSpeed,l = this.getPanes();
        e ? l.fadeOut(e) : l.hide();
        l.eq(d).fadeIn(a.fadeInSpeed, b)
    },slide:function(d, b) {
        this.getPanes().slideUp(200);
        this.getPanes().eq(d).slideDown(400, b)
    },ajax:function(d, b) {
        this.getPanes().eq(0).load(this.getTabs().eq(d).attr("href"), b)
    }},m;
    c.tools.tabs.addEffect("horizontal", function(d, b) {
        m || (m = this.getPanes().eq(0).width());
        this.getCurrentPane().animate({width:0}, function() {
            c(this).hide()
        });
        this.getPanes().eq(d).animate({width:m}, function() {
            c(this).show();
            b.call()
        })
    });
    c.fn.tabs = function(d, b) {
        var a = this.data("tabs");
        if (a) {
            a.destroy();
            this.removeData("tabs")
        }
        if (c.isFunction(b))b =
        {onBeforeClick:b};
        b = c.extend({}, c.tools.tabs.conf, b);
        this.each(function() {
            a = new p(c(this), d, b);
            c(this).data("tabs", a)
        });
        return b.api ? a : this
    }
})(jQuery);
